package com.gtotek.tumchu.activity; 
import java.util.Random; 
import android.content.Context; 
import android.graphics.Bitmap;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.gtotek.tumchu.R;
import com.gtotek.tumchu.base.BaseActivity;
import com.gtotek.tumchu.base.Constans; 
import com.gtotek.tumchu.dao.QuestionDao;
import com.gtotek.tumchu.util.PreferenceUtil;

public class PlayingHiddenActivity extends BaseActivity implements
		View.OnClickListener {
	private ImageView mImgQuestion;

	private RelativeLayout mRltCell1;
	private RelativeLayout mRltCell2;
	private RelativeLayout mRltCell3;
	private RelativeLayout mRltCell4;
	
	private View mViewHelp = mRltCell1;

	@Override
	protected void initialize() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_playing_hidden);
		this.mImgQuestion = (ImageView) this.findViewById(R.id.img_question);
		index = PreferenceUtil.getValue(mContext, Constans.KEY_INDEX_IMAGE, 0);

		// find cell id
		mRltCell1 = (RelativeLayout) this.findViewById(R.id.rltCell1);
		mRltCell2 = (RelativeLayout) this.findViewById(R.id.rltCell2);
		mRltCell3 = (RelativeLayout) this.findViewById(R.id.rltCell3);
		mRltCell4 = (RelativeLayout) this.findViewById(R.id.rltCell4);

		// register event for cell
		mRltCell1.setOnClickListener(this);
		mRltCell2.setOnClickListener(this);
		mRltCell3.setOnClickListener(this);
		mRltCell4.setOnClickListener(this);

		super.initialize();

		// hide help opt
		this.mImgHelp.setClickable(false);
	}

	@Override
	protected void setType() {
		// TODO Auto-generated method stub
		type = QuestionDao.TYPE_ANH;
	}

	@Override
	protected void incrementIndex() {
		// TODO Auto-generated method stub
		PreferenceUtil.setValue(mContext, Constans.KEY_INDEX_IMAGE, index);
		super.incrementIndex();

	}

	@Override
	protected void next() {

		super.next();

		BitmapAjaxCallback ajaxCallback = new BitmapAjaxCallback() {
			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				mProgressBar.setVisibility(View.VISIBLE);
			}

			@Override
			protected void callback(String url, ImageView iv, Bitmap bm,
					AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, iv, bm, status);
				mProgressBar.setVisibility(View.GONE);
			}
		};

		this.mAQuery
				.id(this.mImgQuestion)
				.progress(this.mProgressBar)
				.image(this.mQuestionEntity.getImage(), false, false, 0,
						R.drawable.img_question, ajaxCallback);

		mRltCell1.setVisibility(View.VISIBLE);
		mRltCell2.setVisibility(View.VISIBLE);
		mRltCell3.setVisibility(View.VISIBLE);
		mRltCell4.setVisibility(View.VISIBLE);

		// visible on cell
		Random rand = new Random();
		int pos = rand.nextInt(4) + 1;

		if (pos == 1) {
			mRltCell1.setVisibility(View.INVISIBLE);
		} else if (pos == 2) {
			mRltCell2.setVisibility(View.INVISIBLE);
		} else if (pos == 3) {
			mRltCell3.setVisibility(View.INVISIBLE);
		} else if (pos == 4) {
			mRltCell4.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	protected void passQuestion() {
		// TODO Auto-generated method stub
		// pass sang activity khác
		putToActivityWinOne();
	}

	@Override
	protected void help() {
		// TODO Auto-generated method stub
		if (ruby > 20) {
			 
			mViewHelp.setVisibility(View.INVISIBLE);
			ruby -= 20;
			PreferenceUtil.setValue(mContext, Constans.KEY_RUBY, ruby);
			mTvRuby.setText(ruby + "");
		} else {
			Toast.makeText(mContext, R.string.toast_help_error_message,
					Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		mBuilder.show(); 
		mViewHelp = v;
	}

}
